package com.hsav.kcxb.adapter;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hsav.kcxb.R;
import com.hsav.kcxb.fragment.SongListFragment;
import com.kcbsdk.MyRankBa.ARankTopList;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;


/**
 * Created by zhao on 16-12-10.
 */

public class UserRvAdapter extends RecyclerView.Adapter<UserRvAdapter.MyViewHolder> {

    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    public static final int TYPE_FOOTER = 2;
    private final Context mContext;
    private LayoutInflater mInflater;
    private int mTotal;
    private List<ARankTopList> mRankTopList;
    private OnClickListener mOnClickListener;
    private boolean mIsEnd = false;
    private int mCurrentPage = 0;
    private int mFootViewId = 0;

    public UserRvAdapter(Context context, List<ARankTopList> itemList) {
        this.mContext = context;
        this.mRankTopList = itemList;
        this.mTotal = mRankTopList.size();
        this.mInflater = LayoutInflater.from(context);
        setFooterView(R.layout.item_foot_view);
    }

    private void setFooterView(int footerId) {
        this.mFootViewId = footerId;
    }

    private boolean hasFooterView() {
        return mFootViewId != 0;
    }

    public UserRvAdapter(Context context, int total) {
        this.mContext = context;
        this.mTotal = total;
        this.mInflater = LayoutInflater.from(context);
    }

    public void addData(List<ARankTopList> itemList) {
//        Log.i(SongListFragment.TAG, "add list = " + itemList+"size="+itemList.size());
        if (itemList != null && itemList.size() > 0) {
            if (this.mRankTopList != null) {
                mRankTopList.addAll(itemList);
                this.mTotal = mRankTopList.size();
                mCurrentPage++;
                notifyDataSetChanged();
            }
        } else {
            mIsEnd = true;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mRankTopList != null) {
            if (mRankTopList.size() == position) {
                return TYPE_FOOTER;
            }
        }
        return TYPE_NORMAL;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.i(SongListFragment.TAG, "onCreate view " + viewType);
        if (hasFooterView() && viewType == TYPE_FOOTER) {
            View v = mInflater.inflate(mFootViewId, parent, false);
            return new MyViewHolder(v, true);
        }
        View view = mInflater.inflate(R.layout.adapter_sing_item, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view, false);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        int realPosition = position;
//        Log.i(SongListFragment.TAG, "on bind:" + position);
        if (mRankTopList != null && mRankTopList.size() > realPosition) {
            ARankTopList RankTop = mRankTopList.get(realPosition);
            holder.setData(RankTop, realPosition);
        } else {
            if (!hasFooterView()) {
                holder.setData(null, 0);
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mRankTopList != null) {
            if (hasFooterView() && !mIsEnd) {
                return mRankTopList.size() + 1;
            } else {
                return mRankTopList.size();
            }
        } else {
            return mTotal;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

                @Override
                public int getSpanSize(int position) {
                    if (position == 0) {
                        return 6;
                    } else if (position <= 8) {
                        return 3;
                    } else if (mRankTopList != null && mRankTopList.size() == position) {
                        return 6;
                    } else {
                        return 2;
                    }
                }
            });
        }
    }

    public interface OnClickListener {
        public void onHandleClick(ARankTopList item);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.mOnClickListener = onClickListener;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private View mItemView;
        private TextView mNumberTv;
        private TextView mTitleTv;
        private ImageView mImageView;
        private TextView mNameTv;

        public MyViewHolder(final View itemView, boolean isFootView) {
            super(itemView);
            AutoUtils.auto(itemView);
            if (isFootView) {
                return;
            }
            mImageView = (ImageView) itemView.findViewById(R.id.iv_item_sing);
            mNumberTv = (TextView) itemView.findViewById(R.id.tv_item_number);
            mTitleTv = (TextView) itemView.findViewById(R.id.tv_item_title);
            mNameTv = (TextView) itemView.findViewById(R.id.tv_item_name);
            this.mItemView = itemView;
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnClickListener != null) {
                        ARankTopList item = (ARankTopList) itemView.getTag();
                        mOnClickListener.onHandleClick(item);
                    }
                }
            });

        }

        public void setData(ARankTopList item, int position) {
            if (item != null) {
                mNumberTv.setText(String.valueOf(position) + ".");
                mTitleTv.setText(item.NickName);
                mNameTv.setText(item.SongName);
                Glide.with(mContext).load(item.HeadFile).placeholder(R.drawable.loading_back_new).error(R.drawable.five).into(mImageView);
                mItemView.setTag(item);
            } else {
                mNumberTv.setText("");
                mTitleTv.setText("");
                mNameTv.setText("");
                mImageView.setImageResource(R.drawable.loading_back_new);
            }
            ViewGroup.LayoutParams layoutParams = mItemView.getLayoutParams();
            if (position == 0) {
                layoutParams.height = mContext.getResources().getDimensionPixelSize(R.dimen.item_img_big_height);
            } else if (position <= 8) {
                layoutParams.height = mContext.getResources().getDimensionPixelSize(R.dimen.item_img_normal_height);
            } else {
                layoutParams.height = mContext.getResources().getDimensionPixelSize(R.dimen.item_img_small_height);
            }
            mItemView.setLayoutParams(layoutParams);
        }
    }

    public static class SpaceDecoration extends RecyclerView.ItemDecoration {
        private int mSpace;

        public SpaceDecoration(int space) {
            mSpace = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.top = mSpace;
            outRect.left = mSpace;
        }
    }

    public abstract class LoadMoreListener extends RecyclerView.OnScrollListener {

        private final RecyclerView mRecyclerView;
        private final GridLayoutManager mGridLayoutManager;
        private int mVisibleCount;
        private int mFirstPos;


        public LoadMoreListener(RecyclerView recyclerView) {
            this.mRecyclerView = recyclerView;
            this.mGridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();

        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//            Log.i(SongListFragment.TAG, "scroll state:" + newState);
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            Log.i(SongListFragment.TAG, "on scrolled: dy=" + dy);
            if (dy != 0) {
                this.mVisibleCount = recyclerView.getChildCount();
                this.mFirstPos = mGridLayoutManager.findFirstVisibleItemPosition();
                int lastVisPos = mGridLayoutManager.findLastCompletelyVisibleItemPosition();
                int managerCount = mGridLayoutManager.getItemCount();
                Log.i(SongListFragment.TAG, "last = " + lastVisPos + ",managerCount=" + managerCount + ",visibleCount = " + mVisibleCount + ",firstPos=" + mFirstPos
                        + ",itemcount=" + getItemCount() + ",isEnd=" + mIsEnd);
                if (!mIsEnd && lastVisPos + 1 >= managerCount) {
                    onLoadMore(mCurrentPage + 1);
                }

            }

        }

        public abstract void onLoadMore(int mCurrentPage);
    }

}
